#include "unity.h"
#include "mock_xrt1553B.h"
// #include "mock_ccsds1553.h"
#include "ccsds1553.h"
#include <string.h>



// // Include the source file or declare the function
// extern u32 rt_GetBusCMDData(XRt1553B *InstancePtr, B1553_BusService *pBusServ, u8 *pData, u32 bytes);

// Test variables
XRt1553B testInstance;
B1553_BusService testBusService;
u8 testDataBuffer[256];


/**
 * @brief 测试初始化函数
 */
void setUp(void)
{
    // 初始化测试环境
    memset(&testInstance, 0, sizeof(XRt1553B));
    memset(&testBusService, 0, sizeof(B1553_BusService));
    memset(testDataBuffer, 0, sizeof(testDataBuffer));
}

/**
 * @brief 测试清理函数
 */
void tearDown(void)
{
    // 清理测试环境
}

/**
 * @test TC001: 测试bytes为0的情况
 * @description 当bytes参数为0时，函数应该直接返回0且不调用底层函数
 */
void test_rt_GetBusCMDData_WhenBytesIsZero_ShouldReturnZero(void)
{
    // Arrange
    u32 bytes = 0;
    u32 expected_result = 0;
    
    // 不需要Mock XRt1553B_RxFromSubAddr，因为不应该被调用
    
    // Act
    u32 result = rt_GetBusCMDData(&testInstance, &testBusService, testDataBuffer, bytes);
    
    // Assert
    TEST_ASSERT_EQUAL(expected_result, result);
}

/**
 * @test TC002: 测试正常读取数据的情况
 * @description 当bytes大于0时，应该调用底层函数并返回其结果
 */
void test_rt_GetBusCMDData_WhenBytesGreaterThanZero_ShouldCallRxFunctionAndReturnResult(void)
{
    // Arrange
    u32 bytes = 10;
    u32 mock_return_value = 10;
    u32 expected_result = mock_return_value;
    
    // Mock底层函数，期望被调用一次并返回指定值
    XRt1553B_RxFromSubAddr_ExpectAndReturn(&testInstance, B1553_RT_TXRX_SUBADDR, testDataBuffer, bytes, mock_return_value);
    
    // Act
    u32 result = rt_GetBusCMDData(&testInstance, &testBusService, testDataBuffer, bytes);
    
    // Assert
    TEST_ASSERT_EQUAL(expected_result, result);
}

/**
 * @test TC003: 发送奇数字节 返回偶数字节
 */
void test_rt_GetBusCMDData_WhenRxFails_ShouldReturnErrorCode(void)
{
    // Arrange
    u32 bytes = 5;
    u32 error_code = 4;  
    u32 expected_result = error_code;
    
    
    XRt1553B_RxFromSubAddr_ExpectAndReturn(&testInstance, B1553_RT_TXRX_SUBADDR, testDataBuffer, bytes, error_code);
    
    // Act
    u32 result = rt_GetBusCMDData(&testInstance, &testBusService, testDataBuffer, bytes);
    
    // Assert
    TEST_ASSERT_EQUAL(expected_result, result);
}

/**
 * @test TC004: 测试大字节数读取的情况
 * @description 测试读取较大字节数的情况
 */
void test_rt_GetBusCMDData_WithLargeBytes_ShouldWorkCorrectly(void)
{
    // Arrange
    u32 bytes = 256;
    u32 mock_return_value = 256;
    u32 expected_result = mock_return_value;
    
    // Mock底层函数
    XRt1553B_RxFromSubAddr_ExpectAndReturn(&testInstance, B1553_RT_TXRX_SUBADDR, testDataBuffer, bytes, mock_return_value);
    
    // Act
    u32 result = rt_GetBusCMDData(&testInstance, &testBusService, testDataBuffer, bytes);
    
    // Assert
    TEST_ASSERT_EQUAL(expected_result, result);
}

